/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.platform.filter  
 */
package com.dwusoft.platform.filter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.license.filter.AbstractFilter;
import com.apache.task.api.ScheduledExecutor;

/**
 * description:  系统license验证类
 * @author Hou Dayu 创建时间：2014-12-23  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class LicenseFilter extends AbstractFilter {

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#destroy()  
	 */
	@Override
	public void destroy() {

	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)  
	 */
	@Override
	public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain arg2) throws IOException,
			ServletException {
		HttpServletRequest request = (HttpServletRequest) srequest;
		HttpServletResponse response = (HttpServletResponse) sresponse;
		//CookeHeader(response, request);

		arg2.doFilter(request, response);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)  
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.initLincense();
		ScheduledExecutor.newInstance().initStart();
	}

	/**
	 * description:  修复会话cookie 中缺少HttpOnly属性漏洞
	 */
	private void CookeHeader(HttpServletResponse response, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (!isNull(cookies)) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.HOUR, 1);
			Date date = cal.getTime();
			Locale locale = Locale.CHINA;
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", locale);
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if (cookie != null) {
					StringBuilder builder = new StringBuilder();
					if (cookie.getSecure())
						builder.append("Secure;");
					String value = cookie.getValue();
					if ("JSESSIONID".equals(cookie.getName())) {
						String path = request.getContextPath();
						builder.append("JSESSIONID=" + value + ";");
						builder.append("path=" + path + ";");
						builder.append("HttpOnly;");
						builder.append("Expires=" + sdf.format(date));
						response.addHeader("Set-Cookie", builder.toString());
						return;
					}
				}
			}
		}
	}

	private boolean isNull(Object[] object) {
		if (object == null || object.length == 0) {
			return true;
		} else {
			return false;
		}
	}
}
